<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.4/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
<button type="button" id="add" class="layui-btn layui-btn-warm"
        lay-submit="" lay-filter="add">新增员工</button>
<table class="layui-hide" id="test" lay-filter="test"></table>

<script src="../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" href="Client/client_management.html">查看</a>
</script>



<script src="../lib/layui-v2.5.4/layui.js" charset="utf-8">
</script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>

    layui.use(['table', 'form'], function () {

        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;

        table.render({
            elem: '#test'
            , url: '/api/emp/emplist'
            //, toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,where:{"eId":1}
            , title: '员工表',
                request: {
                    pageName: 'current' //页码的参数名称，默认：page
                    , limitName: 'size' //每页数据量的参数名，默认：limit
                }
            , cols: [[
                {field: 'eId', title: '员工编号', sort: true,align: "center",line:"center"}
                , {field: 'eTruename', title: '员工真实名称', align: "center"}
                , {field: 'pName', title: '所在部门', align: "center"}
                , {field: 'eLoginname', title: '登录名', align: "center"}
                , {field: 'ePsw', title: '登录密码', align: "center"}
                , {field: 'eSex', title: '员工性别', align: "center"}
                , {field: 'eImg', title: '员工头像',align:"center" }
                , {field: 'eFlag', title: '员工类别', align: "center"}
                , {field: 'eAdmin', title: '权限级别', align: "center"}
                , {field: 'eRemark', title: '操作', toolbar: '#barDemo',align: "center"
            }
            ]],
            limits: [5, 10, 15],
            limit: 5,
                page: true
        });
        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });
        $("#add").click(function(){

            layer.open({
                type: 2 //此处以iframe举例
                ,title: '添加员工'
                ,area: ['390px', '260px']
                ,shade: 0
                ,maxmin: true
                ,content: 'addemployees.html'
                ,btn: ['提交', '关闭']
                ,yes: function(index){
                    debugger;
                    var data = window['layui-layer-iframe' + index].getValue();
                    if (!data.name) {
                        layer.msg("员工名称不能为空");
                        return false;
                    }

                    //发送新增请求 get/post √
                    axios.post('/api/emp/empAdd', data).then(function (response) {
                        //post请求执行成功的回调
                        if (response.data.status == "0") {
                            layer.close(index);
                            //刷新表格
                            tableReload();
                            layer.msg("新增成功");
                        } else {
                            layer.msg(response.data.msg)
                        }
                    }).catch(function (error) {
                        console.log(error);
                    });
                }
                ,btn2: function(index){//5
                    layer.close(index);
                }
            });
        })

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.prompt({
                    formType: 2
                    , value: data.email
                }, function (value, index) {
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>

</body>
<style>
    .layui-table-cell{
       height: 60px;
        line-height:60px;
    }

</style>
</html>